﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIPO.Library.DomainObject.Administration;
using SIPO.Library.DataAccess.Administration;
using SIPO.Library.DomainObject.Transaction;
using SIPO.Library.DataAccess.Transaction;
using System.Configuration;
using SIPO.Library.DataAccess.Administration.Catalog;
using SIPO.Library.DomainObject.Administration.Catalog;
using SIPO.Web.Support;
using SIPO.Library.DomainObject.Administration.UserManagement;
using System.Globalization;

namespace SIPO.Web.Forms.Transaction
{
    public partial class recSanctionOpenform : BasePage
    {
        private int id = -1;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (Request.Params["id"] != null)
            {
                id = Convert.ToInt32(Request.Params["id"]);
            }

            if (!Page.IsPostBack)
            {

                if (id > 0) // Edit an existing item
                {
                    recSanctionRepo recSanctionDal = new recSanctionRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                    recSanction recSanction = recSanctionDal.GetById(id);
                    if (recSanction != null)
                    {
                        hfID.Value = id.ToString();
                        txtLetterNo.Text = recSanction.LetterNo;
                        txtLetterDate.Text = recSanction.LetterDate.HasValue ? recSanction.LetterDate.Value.ToString("dd/MM/yyyy") : string.Empty;
                        txtValue.Text =  recSanction.Value.ToString();
                        //txtValue.Text = Helper.ToDecimal(recSanction.Value.ToString(), NumberStyles.Currency, Helper.Culture.Indonesian).ToString();// sanction.Value.ToString();
                        txtInvoiceNo.Text = recSanction.InvoiceNo;
                        txtFileName.Text = recSanction.AttachedFileName;
                        txtInvoiceDate.Text = recSanction.InvoiceDate.HasValue == true ? recSanction.InvoiceDate.Value.ToString("dd/MM/yyyy") : string.Empty;
                        txtInvoiceNo.Text = recSanction.InvoiceNo;
                        txtReviewBy.Text = recSanction.ReviewBy;
                        txtReviewDate.Text = recSanction.ReviewDate.HasValue == true ? recSanction.ReviewDate.Value.ToString("dd/MM/yyyy") : string.Empty;
                        txtPaymentDate.Text = recSanction.PaymentDate.HasValue == true ? recSanction.PaymentDate.Value.ToString("dd/MM/yyyy") : string.Empty;
                        txtPaymentNo.Text = recSanction.PaymentNo;
                        txtApproveBy.Text = recSanction.ApproveBy;
                        txtApproveDate.Text = recSanction.ApproveDate.HasValue == true ? recSanction.ApproveDate.Value.ToString("dd/MM/yyyy") : string.Empty;

                        if (recSanction.DebtorSectorID>0)
                        {
                            //ambil data debtor sector
                            recDebtorSectorRepo recDebtorSectorDal = new recDebtorSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                            recDebtorSector objDebtorSector = recDebtorSectorDal.GetById(recSanction.DebtorSectorID);
                            if (objDebtorSector != null)
                            {
                                //ambil data debtor
                                recDebtorRepo recDebtorDal = new recDebtorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                                recDebtor objDebtor = recDebtorDal.GetById(objDebtorSector.DebtorID);
                                if (objDebtor != null)
                                {
                                    txtDebitur.Text = objDebtor.DebtorCode + " - " + objDebtor.DebtorName;
                                }


                                //ambil data sub sector
                                recSubSectorRepo recSubSectorDal = new recSubSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                                recSubSector objSubSector = recSubSectorDal.GetById(objDebtorSector.SubSectorID);
                                if (objSubSector != null)
                                {
                                    txtSubSector.Text = objSubSector.SubSectorCode + " - " + objSubSector.SubSectorName;
                                }

                                //ambil data sub sub sector
                                recSubSubSectorRepo recSubSubSectorDal = new recSubSubSectorRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                                recSubSubSector objSubSubSector = recSubSubSectorDal.GetById(objDebtorSector.SubSubSectorID);
                                if (objSubSubSector != null)
                                {
                                    txtSubSubSector.Text = objSubSubSector.SubSubSectorCode + " - " + objSubSubSector.SubSubSectorName;
                                }
                            }


                        }

                        //ambil data sanksi
                        if (recSanction.SanctionTypeID > 0)
                        {
                            recSanctionTypeRepo recSanctionTypeDal = new recSanctionTypeRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                            recSanctionType objSanctionType = recSanctionTypeDal.GetById(recSanction.SanctionTypeID);

                            if (objSanctionType != null)
                            {
                                txtTransType.Text = objSanctionType.SanctionTypeName;
                            }
                        }

                        //if (recSanction.InvoiceNo.Trim().Length > 1)
                        //    trInvoice.Visible = true;

                        //if (recSanction.InvoiceDate != null && recSanction.InvoiceDate.Value.ToString().Trim().Length > 1)
                        //    trInvoiceDate.Visible = true;

                        //if (recSanction.PaymentNo.Trim().Length > 1)
                        //    trPayment.Visible = true;

                        //if (recSanction.PaymentDate != null && recSanction.PaymentDate.ToString().Trim().Length > 1)
                        //    trPaymentDate.Visible = true;

                        trReviewDate.Visible = recSanction.ReviewDate.HasValue ? true : false;
                        trReviewBy.Visible = recSanction.ReviewBy.Trim().Length > 0 ? true : false;
                        trInvoice.Visible = recSanction.InvoiceNo.Trim().Length > 0 ? true : false;
                        trInvoiceDate.Visible = recSanction.InvoiceDate.HasValue ? true : false;
                        trPayment.Visible = recSanction.PaymentNo.Trim().Length > 0 ? true : false;
                        trPaymentDate.Visible = recSanction.PaymentDate.HasValue ? true : false;
                        trApproveBy.Visible = recSanction.ApproveBy.Trim().Length > 0 ? true : false;
                        trApproveDate.Visible = recSanction.ApproveDate.HasValue ? true : false;

                        hfInvoiceNo.Value = recSanction.InvoiceNo.Trim().Length>0?recSanction.InvoiceNo:string.Empty;
                        hdReviewDate.Value = recSanction.ReviewDate.HasValue ? recSanction.ReviewDate.ToString() : string.Empty;
                        hdApproveDate.Value = recSanction.ApproveDate.HasValue ? recSanction.ApproveDate.ToString() : string.Empty;
                    }
                }

               GetPrivilege();
            }

        }

        private void GetPrivilege()
        {
            Membership membership = new Membership(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

            List<recAccessRights.AccessRights> accessRights = membership.GetAccessRights(base.UserID, base.MenuID);

            accessRights.ForEach(delegate(recAccessRights.AccessRights access)
            {
                switch (access)
                {
                    case (recAccessRights.AccessRights.View):
                        ClientScript.RegisterStartupScript(typeof(Page), "view", "$('#btnSave').hide();", true);
                        break;
                    case (recAccessRights.AccessRights.Add):
                        ClientScript.RegisterStartupScript(typeof(Page), "add", "$('#btnSave').show();", true);
                        break;
                    case (recAccessRights.AccessRights.Edit):
                        ClientScript.RegisterStartupScript(typeof(Page), "edit", "$('#btnSave').show();", true);
                        break;
                    case (recAccessRights.AccessRights.NoAccess):
                        var url = Helper.GetAppVirtualPath(Page);
                        ClientScript.RegisterStartupScript(typeof(Page), "noAccess", "alert('Anda tidak mempunyai hak akses ke halaman ini!'); window.location = '" + url + "'", true);
                        break;
                }
            });

        }

    }
}
